'''
Created on Sep 21, 2012

@author: ieb
'''
from controllers.utils import BaseHandler, SITE_SETUP
from models.models import Course, Unit
import datetime
import time
import logging


class NewCourseHandler(BaseHandler):
    def post(self):
        coursecode = self.request.POST["coursecode"]
        coursename = self.request.POST["coursename"]
        
        course = Course.all().filter("coursecode=", coursecode).get()
        if course is None:
            site = SITE_SETUP.get_site_setup()
            course = Course(coursecode=coursecode,
                            coursename=coursename,
                            full_coursename=" Click to edit full name of %s " % coursename,
                            start_date= datetime.date.today(),
                            inst_url=site['inst_url'] ,
                            inst_url_footer=site['inst_url_footer'],
                            inst_name=site['inst_name'],
                            inst_logo=site['inst_logo'],
                            header_phrase="[ Edit the header phrase of %s ]" % coursename,
                            privacy_url_footer=site['privacy_url_footer'],
                            analytics_id=site['analytics_id'],
                            group_collaboration='[collab-%s]' % coursename,
                            course_description='[ Edit the course description for %s ]' % coursename,
                            course_instructor='[ Edit the instructor for %s]' % coursename,
                            video_introduction=site['video_introduction'],
                            banner_image=site['banner_image'],
                            regisrtration_questions= '{ "q1" : "Whats your name ?" }',
                            certificate_of_completion="FALSE")
            course.put()
            self.response.write("Ok")
        else:
            self.response.status_int = 419
            self.response.write("Course code already exists")

class UpdateCourseHandler(BaseHandler):
    
    def _safe_html(self, unsafe):
        return unsafe # Make safe.
    
    def post(self, coursecode, field):
        course = self.get_course()
        if field == 'coursename':
            course.coursename = self.request.POST['value']
        elif field == 'full_coursename':
            course.full_coursename = self.request.POST['value']
        elif field == 'start_date':
            t = time.strptime(self.request.POST['value'],"%Y-%m-%d")
            course.start_date = datetime.date(t.tm_year, t.tm_mon, t.tm_mday)
        elif field == 'inst_url':
            course.inst_url = self.request.POST['value']
        elif field == 'inst_url_footer':
            course.inst_url_footer = self.request.POST['value']
        elif field == 'inst_name':
            course.inst_name = self.request.POST['value']
        elif field == 'inst_logo':
            course.inst_logo = self.request.POST['value']
        elif field == 'header_phrase':
            course.header_phrase = self.request.POST['value']
        elif field == 'privacy_url_footer':
            course.privacy_url_footer = self.request.POST['value']
        elif field == 'analytics_id':
            course.analytics_id = self.request.POST['value']
        elif field == 'group_collaboration':
            course.group_collaboration = self.request.POST['value']
        elif field == 'group_announcements':
            course.group_announcements = self.request.POST['value']
        elif field == 'course_description':
            course.course_description = self.request.POST['value']
        elif field == 'course_instructor':
            course.course_instructor = self.request.POST['value']
        elif field == 'course_instructor':
            course.course_instructor = self.request.POST['value']
        elif field == 'video_introduction':
            course.video_introduction = self.request.POST['value']
        elif field == 'banner_image':
            course.banner_image = self.request.POST['value']
        elif field == 'registration_questions':
            course.registration_questions = self.request.POST['value']
        elif field == 'certificate_of_completion':
            course.certificate_of_completion = self.request.POST['value']
        else:
            self.response.status_code = 400
            return
        logging.error("Saving course %s " % course)
        self.save_course(course)
        self.response.headerlist = [('Content-type', 'text/html; charset=utf8;')]
        self.response.write(self._safe_html(self.request.POST['value']))



class NewUnitHandler(BaseHandler):
    def post(self, coursecode):
        logging.error("Saving new unit under course %s" % coursecode)
        unit_id = int(self.request.POST["unit_id"])
        title = self.request.POST["title"]
        unit_type = self.request.POST["unit_type"]
        unit_extra = self.request.POST["unit_extra"]
        course = self.get_course()
        if course is None:
            logging.error("Failed to find course code %s" % coursecode)
            self.response.status_int = 404
            self.response.write("Course not found")
            return
        unit = self.get_unit(unit_id);
        if unit is not None:
            self.response.status_int = 419
            self.response.write("Unit already exists")
            return
        unit = Unit(
            coursecode = course.coursecode,
            type=unit_type,
            unit_id=unit_id,
            unit_extra=unit_extra,
            title=title,
            release_date="",
            now_avaialble=True
            )
        unit.put()
        self.response.write("Ok")
        